﻿Imports System.Data.SqlClient
Imports System.Data.OleDb
Imports System.Data

Partial Class ReservaPlat
    Inherits System.Web.UI.Page

    Public Function cantCheckSeleccionados(ByVal grilla As GridView) As Integer
        'Dim fila As GridViewRow
        Dim Check As CheckBox
        Dim cantSel As Integer
        'for para verificar que se selecciono algo
        For Each fila In grilla.Rows
            Check = CType(fila.Cells(0).Controls(1), CheckBox)
            If (Check.Checked) And (Check.Enabled) Then
                cantSel = cantSel + 1
            End If
        Next
        Return cantSel
    End Function

    Public Sub insertarCheckSeleccionados(ByVal grilla As GridView, ByVal nroMaquina As Integer)
        Dim Check As CheckBox
        For Each fila In grilla.Rows
            Check = CType(fila.Cells(0).Controls(1), CheckBox)
            'pregunta si hay alguno q este checkeado y habilitado
            If (Check.Checked) And (Check.Enabled) Then
                Dim t As New Turnos
                Try
                    'hace la reserva llamando al procedimiento almacenado
                    t.NuevoTurno(nroMaquina, Session("dniSocio").ToString, _
                                 Me.Calendar1.SelectedDate, fila.Cells.Item(1).Text)
                    actualizarGrillaHorario(Calendar1.SelectedDate, grilla, nroMaquina)
                Catch ex As Exception
                    MsgBox("Error de BD!", vbMsgBoxSetForeground, "AAHH")
                End Try
            End If
        Next
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        'si se selecciono algo cantCheckSeleccionados es mayor que 0
        If (cantCheckSeleccionados(Me.GridViewHoriarios) > 0) Or _
            (cantCheckSeleccionados(Me.GridViewHoriarios2) > 0) Then
            'recorre la grilla de horarios e inserta los check seleccionados y habilitados
            insertarCheckSeleccionados(Me.GridViewHoriarios, 1)
            insertarCheckSeleccionados(Me.GridViewHoriarios2, 2)
        Else
            MsgBox("Debe Seleccionar un Horario", vbMsgBoxSetForeground, "AAHH")
        End If
    End Sub

    Public Sub actualizarGrillaHorario(ByVal fecha As DateTime, ByVal grilla As GridView, ByVal nroMaquina As Integer)
        Dim t As New Turnos
        grilla.DataSource = t.obtenerHorarios()
        grilla.DataBind()
        deshabilitarHorario(fecha, grilla, nroMaquina)
        resaltarHorario(Calendar1.SelectedDate, Session("dniSocio").ToString, grilla, nroMaquina)
    End Sub

    Public Sub deshabilitarHorario(ByVal fecha As DateTime, ByVal grilla As GridView, ByVal nroMaquina As Integer)
        Dim fila As GridViewRow
        Dim Check As CheckBox
        Dim t As New Turnos
        Dim ds As New DataSet
        ds = t.obtenerHorarios(fecha, nroMaquina)
        'for para recorrer toda la grilla de horarios
        For Each fila In grilla.Rows
            'guardo el checkbox de la fila actual
            Check = CType(fila.Cells(0).Controls(1), CheckBox)
            'for para recorrer todas las reservas de la fecha seleccionada q esta en ds.
            For i = 0 To ds.Tables(0).Rows.Count - 1
                If fila.Cells(1).Text = ds.Tables(0).Rows(i).Item(0).ToString Then
                    'deshabilita el checkbox si ya esta en la BD
                    Check.Enabled = False
                    'checked al checkbox
                    Check.Checked = True
                    'tacha el texto con <srike>
                    fila.Cells(2).Text = "<strike>" + fila.Cells(2).Text + "</strike>"
                End If
            Next
        Next
    End Sub

    Public Sub resaltarHorario(ByVal fecha As DateTime, ByVal dni As String, ByVal grilla As GridView, ByVal nroMaquina As Integer)
        Dim fila As GridViewRow
        Dim Check As CheckBox
        Dim botonQuitar As LinkButton
        Dim t As New Turnos
        Dim ds As New DataSet
        ds = t.obtenerHorarios(fecha, dni, nroMaquina)

        For Each fila In grilla.Rows
            Check = CType(fila.Cells(0).Controls(1), CheckBox)
            botonQuitar = CType(fila.Cells(0).Controls(3), LinkButton)
            For i = 0 To ds.Tables(0).Rows.Count - 1
                If fila.Cells(1).Text = ds.Tables(0).Rows(i).Item(0).ToString Then
                    Check.Enabled = False
                    Check.Checked = True
                    fila.Cells(2).Text = "<strike>" + fila.Cells(2).Text + "</strike>"
                    'Resalta la fila que es la del usuario actual
                    fila.BackColor = Drawing.Color.Pink
                    'Habilita el boton para que pueda quitar su reserva
                    botonQuitar.Visible = True
                End If
            Next
        Next
    End Sub


    Protected Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) Handles Calendar1.DayRender
        Dim t As New Turnos
        Dim ds As New DataSet
        ds = t.obtenerHorarios(e.Day.Date, Session("dniSocio").ToString)
        If ds.Tables(0).Rows.Count > 0 Then
            e.Cell.ForeColor = System.Drawing.Color.Red
        End If
    End Sub



    Protected Sub Calendar1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Calendar1.SelectionChanged
        actualizarGrillaHorario(Calendar1.SelectedDate, Me.GridViewHoriarios, 1)
        actualizarGrillaHorario(Calendar1.SelectedDate, Me.GridViewHoriarios2, 2)
        'El ultimo parametro representa la maquina
    End Sub

    Protected Sub GridViewHoriarios_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridViewHoriarios.RowCreated
        e.Row.Cells(1).Visible = False 'Oculta el id en la grilla de horarios
    End Sub

    Protected Sub GridViewHoriarios2_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridViewHoriarios2.RowCreated
        e.Row.Cells(1).Visible = False 'Oculta el id en la grilla de horarios
    End Sub

    Public Sub LinkButtonQuitar_1_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim lnk As LinkButton
        lnk = CType(sender, LinkButton)
        Dim t As New Turnos
        Dim fila As GridViewRow
        fila = CType(lnk.NamingContainer, GridViewRow)
        fila = lnk.NamingContainer 'Obtiene la fila
        Try
            'LLama al procedimiento almacenado de quitar Turno
            t.QuitarTurno(1, Session("dniSocio").ToString, Me.Calendar1.SelectedDate, fila.Cells.Item(1).Text)
            actualizarGrillaHorario(Calendar1.SelectedDate, Me.GridViewHoriarios, 1)
        Catch ex As Exception
            MsgBox("Error de BD!", vbMsgBoxSetForeground, "AAHH")
        End Try
    End Sub

    Public Sub LinkButtonQuitar_2_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim lnk As LinkButton
        lnk = CType(sender, LinkButton)
        Dim t As New Turnos
        Dim fila As GridViewRow
        fila = CType(lnk.NamingContainer, GridViewRow)
        fila = lnk.NamingContainer 'Obtiene la fila
        Try
            'LLama al procedimiento almacenado de quitar Turno
            t.QuitarTurno(2, Session("dniSocio").ToString, Me.Calendar1.SelectedDate, fila.Cells.Item(1).Text)
            actualizarGrillaHorario(Calendar1.SelectedDate, Me.GridViewHoriarios2, 2)
        Catch ex As Exception
            MsgBox("Error de BD!", vbMsgBoxSetForeground, "AAHH")
        End Try
    End Sub

End Class
